package com.cc.micro.dataService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cc.micro.api.model.BidInfo;
import com.cc.micro.api.model.LoanInfo;
import com.cc.micro.api.service.PlatBaseInfoService;
import com.cc.micro.api.vo.BaseInfo;
import com.cc.micro.dataService.mapper.BidInfoMapper;
import com.cc.micro.dataService.mapper.LoanInfoMapper;
import com.cc.micro.dataService.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

/**
 * @author CC
 * time 2023-07-01
 * description
 */
@DubboService(interfaceClass = PlatBaseInfoService.class,version = "1.0")
public class PlatBaseInfoServiceImpl implements PlatBaseInfoService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoanInfoMapper loanInfoMapper;
    @Autowired
    private BidInfoMapper bidInfoMapper;
    @Override
    public BaseInfo queryPlatBaseInfo() {
        //获取注册人数,收益率平均值，累计成交金额
        Integer integer = userMapper.selectCount(null);
        Map<String, Object> map = loanInfoMapper.selectMaps(
                new QueryWrapper<LoanInfo>().
                        select("avg(rate) as avgRate")).get(0);
        BigDecimal avgRate = (BigDecimal) map.get("avgRate");
        avgRate=avgRate==null?null:avgRate.setScale(2, RoundingMode.HALF_DOWN);
        Map<String, Object> map1 = bidInfoMapper.selectMaps(
                new QueryWrapper<BidInfo>().
                        select("sum(bid_money) as sumBidMoney")).get(0);
        BigDecimal sumBidMoney = (BigDecimal) map1.get("sumBidMoney");
        return new BaseInfo(avgRate,sumBidMoney,integer);
    }



}
